package com.zhiyou100.controller.user;

import com.zhiyou100.dao.Impl.UserDaoImpl;
import com.zhiyou100.emtity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @description:
 * @author: 马跃
 * @create: 2021-01-20 16:47
 **/
@WebServlet("/userUpdate")
public class UserUpdateServlet extends HttpServlet {

    User query;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String userName = request.getParameter("userName");
        String oldPassword = request.getParameter("oldPassword");
        String newPassword = request.getParameter("newPassword");
        String confirmPassword = request.getParameter("confirmPassword");
        if (newPassword.equals(confirmPassword)){
            if (query.getU_password().equals(oldPassword)&&query.getU_account().equals(userName)){
                query.setU_password(newPassword);
                new UserDaoImpl().upDate(query);
                request.getRequestDispatcher("/userQueryAll").forward(request, response);
            }else {
                response.getWriter().print("<font style='color:red;font-size:40'>老账号密码不一致</font>");
                response.getWriter().print("<br><a href='/userQueryAll'>返回主页</a>");
            }
        }else {
            response.getWriter().print("<font style='color:red;font-size:40'>俩次密码不一致</font>");
            response.getWriter().print("<br><a href='/userQueryAll'>返回主页</a>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        query= new UserDaoImpl().query(id);
        request.getRequestDispatcher("./Adminastrator/user/updateUser.html").forward(request, response);
    }
}
